package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hhs.storage.bean.JxcMenuRole;
import com.hhs.storage.bean.JxcRole;
import com.hhs.storage.bean.Result;
import com.hhs.storage.bean.YymHuiyuanTc;
import com.hhs.storage.dao.RoleDao;
import com.hhs.storage.service.JxcMenuRoleService;
import com.hhs.storage.service.RoleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hhs.storage.service.YymHuiyuanTcService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shadow
 * @since 2018-09-29
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, JxcRole> implements RoleService {
    @Resource
    JxcMenuRoleService jxcMenuRoleService;
    @Resource
    YymHuiyuanTcService yymHuiyuanTcService;
    @Override
    public Map<String, Object> dele(String id) {
        //先删除关联菜单数据
        jxcMenuRoleService.delete(new EntityWrapper<JxcMenuRole>().eq("role_id_", id));
        this.delete(new EntityWrapper<JxcRole>().eq("id_", id));
        //
        List<YymHuiyuanTc> list = yymHuiyuanTcService.selectList(new EntityWrapper<YymHuiyuanTc>().eq("role_id_", id));
        for (YymHuiyuanTc item :
                list) {
            item.setRoleId("");
            yymHuiyuanTcService.insertOrUpdate(item);
        }
        return null;
    }

    @Override
    public Map<String, Object> putMenus(String roleId, String menuIds) {
        //删除所有旧数据，添加新数据
        jxcMenuRoleService.delete(new EntityWrapper<JxcMenuRole>().eq("role_id_", roleId));
        String[] idArr =  menuIds.split(",");
        for (String s : idArr) {
            if(s.equals("null")) continue;
            JxcMenuRole temp = new JxcMenuRole();
            temp.setRoleId(roleId);
            temp.setMenuId(s);
            jxcMenuRoleService.insert(temp);
        }
        return null;
    }

    @Override
    public Map<String, Object> getByUserId(String userId) {
        List<JxcRole> list = this.baseMapper.getByUserId(userId);
        return Result.sucJsonResp(list);
    }
}
